package controller

import (
	"fx_swift_ifcgate/api"
	"fx_swift_ifcgate/internal/service"
	"fx_swift_ifcgate/pkg/code"
	"fx_swift_ifcgate/pkg/enum"
	"fx_swift_ifcgate/pkg/internalerrors"
	"fx_swift_ifcgate/pkg/validator"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateVAInfo(c *gin.Context) {
	req := &api.VaInfoReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, validator.GetErrorMsg(req, err))
		return
	}

	err := service.NewVAInfoSrv().CreateVAInfo(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 获取申请大类
func QueryParentCode(c *gin.Context) {
	result, err := enum.InitParentCode()
	if err != nil {
		return
	}
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取申请子类
func QueryApplyCode(c *gin.Context) {
	parentCode := c.Query("parentCode")
	result, err := service.NewVAInfoSrv().QueryApplyCode(c, parentCode)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取收款币种
func QueryCurrencyCode(c *gin.Context) {
	req := &api.CurrencyReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrBind, err.Error())
		return
	}

	result, err := service.NewVAInfoSrv().QueryCurrencyCode(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取va开户回调的数据
func QueryVAInfoCallBackList(c *gin.Context) {
	result, err := service.NewVAInfoSrv().QueryVAInfoCallBackList(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取va开户资料信息
func QueryVAInfo(c *gin.Context) {
	merOrderNo := c.Query("merOrderNo")
	result, err := service.NewVAInfoSrv().QueryVAInfo(c, merOrderNo)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取交易类型
func GetPaymentType(c *gin.Context) {
	paymentType := []*api.PaymentTypeRes{
		{Code: "B2B", Desc: enum.B2B},
		{Code: "B2C", Desc: enum.B2C},
		{Code: "C2B", Desc: enum.C2B},
		{Code: "C2C", Desc: enum.C2C},
	}
	code.Success(c, paymentType)
}

// 获取交易编码
func GetTradeCodeType(c *gin.Context) {
	result, err := service.NewVAInfoSrv().GetTradeCodeType(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
